<?php
namespace app\flash\controller;
use QL\QueryList;
use think\Db;
use think\Controller;
use think\Exception;
use think\Common;
use think\Validate;

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
//header('Access-Control-Allow-Methods: POST,GET','OPTIONS');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http动作
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
    exit;
}
class UserHangqing extends Base
{
    public function __construct()
    {
        parent::__construct(); // TODO: Change the autogenerated stub
        $this->user;
    }

    //自选列表
   public function index(){
        $user = $this->user;
        $currency = db('currency_hangqing_zixuan')
            ->alias('chz')
            ->join('currency c','chz.hid=c.id')
            ->join('currency_hangqing ch','c.code=ch.code')
            ->where(array('chz.uid'=>$user['id']))
            ->field('ch.id,chz.hid,c.symbol,ch.current_price,ch.current_price_usd,ch.change_percent,c.code')
            ->select();
       return json(msg(1,$currency,'自选行情返回成功'));
   }

   //加入自选
   public function addzixuan(){
       $input = input('');
       $validate = Validate::make([
           'hid'  => 'require',
       ]);
       $res = $validate->check($input);
       if($res !== true){
           return json(msg(0, '', $validate->getError()));
       }
       $input['uid'] = $this->user['id'];
       $find = db('currency_hangqing_zixuan')->where($input)->find();
       if($find){
           return json(msg(0, '', '该货币已加入自选不可重复插入'));
       }

        $res = db('currency_hangqing_zixuan')->insertGetId($input);
        if($res){
            return json(msg(1, '', '插入自选成功'));
        }else{
            return json(msg(0, '', '插入自选失败'));
        }
   }

   //删除自选
    public function delzixuan(){
        $input = input('');
        $validate = Validate::make([
            'id'  => 'require',
        ]);
        $res = $validate->check($input);
        if($res !== true){
            return json(msg(0, '', $validate->getError()));
        }
        $find = db('currency_hangqing_zixuan')->where($input)->find();
        if(!$find){
            return json(msg(0, '', '未找到该条自选'));
        }
        $res = db('currency_hangqing_zixuan')->where(array('id'=>$input['id']))->delete();
        if($res){
            return json(msg(1, '', '删除自选成功'));
        }else{
            return json(msg(0, '', '删除自选失败'));
        }
    }
    /**
     * 添加删除自选
     * 
     */
    public function adddelzixuan(){
        $input = input('');
        $validate = Validate::make([
            'hid'  => 'require',
            'type' => 'require',
        ]);
        $res = $validate->check($input);
        if ($res !== true) {
            return json(msg(0, '', $validate->getError()));
        }
        $type =$input['type'];
        unset($input['type']);
        if ($type==1) {
            $input['uid'] = $this->user['id'];
            $find = db('currency_hangqing_zixuan')->where($input)->find();
            if ($find) {
                return json(msg(0,'','该货币已加入自选不可重复插入'));
            }
            $res = db('currency_hangqing_zixuan')->insertGetId($input);
            if($res){
                return json(msg(1, '', '插入自选成功'));
            }else{
                return json(msg(0, '', '插入自选失败'));
            }
        }else{
            $find = db('currency_hangqing_zixuan')->where($input)->find();
            if(!$find){
                return json(msg(0, '', '未找到该条自选'));
            }
            $input['uid'] = $this->user['id'];
            $res = db('currency_hangqing_zixuan')->where(array('uid'=>$input['uid'],'hid'=>$input['hid']))->delete();
            if($res){
                return json(msg(1, '', '删除自选成功'));
            }else{
                return json(msg(0, '', '删除自选失败'));
            }
        }
    }
}
